package com.pic.mgt.syushin;

import java.io.File;
import java.io.IOException;

import org.apache.commons.lang.StringUtils;

import com.pic.mgt.monitor.MessageUnitFactory;
import com.pic.mgt.monitor.RedundantConfirmationFileException;
import com.pic.mgt.monitor.UnitConverter;
import com.pic.mgt.monitor.WrongUnitFormatException;
import com.pic.mgt.syushin.unit.BucketOkSyushinMessageUnit;
import com.pic.mgt.syushin.unit.RealTargetOkSyushinMessageUnit;
import com.pic.mgt.unit.Message;
import com.pic.mgt.unit.Unit;

public final class SyushinMessageUnitFactory extends MessageUnitFactory {
	
	private int realMessageFilenameLen;
	private int bucketMessageFilenameLen;
	private UnitConverter realSyushinMessageUnitConverter;
	private UnitConverter batchSyushinMessageUnitConverter;
	
	@Override
	protected Unit createIGXTypeUnit(File confirmationFile,
									 String messageTypeString,
									 String businessTypeString)
									 throws IOException, WrongUnitFormatException,
									 RedundantConfirmationFileException {
		// 先取得空的訊息物件
		Unit messageUnit = getEmptyMessage(confirmationFile.getName(),
										   messageTypeString,
										   businessTypeString);
		
		if (null != messageUnit) {
			if (RealTargetOkSyushinMessageUnit.class.isInstance(messageUnit)) {
				// 即時集信交易
				
				messageUnit = 
					this.realSyushinMessageUnitConverter.
									parseToUnit(confirmationFile, messageUnit);
			}
			else if(BucketOkSyushinMessageUnit.class.isInstance(messageUnit)) {
				messageUnit = 
					this.batchSyushinMessageUnitConverter.
									parseToUnit(confirmationFile, messageUnit);
			}
		}
		else {
			// 找不到對應的訊息類別
			throw new WrongUnitFormatException(
					"Can't find match message type '"
							+ messageTypeString
							+ "' to create message unit for confirmation file '"
							+ confirmationFile.getName() + "'.");
		}
		
		return messageUnit;
	}
	
	protected Message getEmptyMessage(String confirmationFilename,
									  String messageTypeString,
									  String messgaeIGXTypeString) {
		Message emptyMessage = null;
		
		// 取得交易檔的檔案長度
		int transactionFilenameLen = StringUtils.removeEnd(confirmationFilename, messageTypeString)
							         .length();
		
		if (transactionFilenameLen == this.realMessageFilenameLen) {
			// 表示為 real 的 Message
			
			if (messageTypeString.endsWith(super.TRANSOK_MESSAGE)) {
				// 如果是屬於 Trans Ok 的 Message
				emptyMessage = new RealTargetOkSyushinMessageUnit();
			}
			else {
				// 如果是屬於 其它的  Message Type (ex: Trans NG ...)
			}
		}
		else if (transactionFilenameLen == this.bucketMessageFilenameLen) {
			//　表示為 bucket 的 Message
			
			if (messageTypeString.endsWith(super.TRANSOK_MESSAGE)) {
				// 如果是屬於 Trans Ok 的 Message
				emptyMessage = new BucketOkSyushinMessageUnit();
			}
			else {
				// 如果是屬於 其它的  Message Type (ex: Trans NG ...)
			}
		}
		else {
			// Message 格式有錯
		}
		
		return emptyMessage;
	}
			
	// --- getter and setter ---

	public void setRealSyushinMessageUnitConverter(
			UnitConverter realSyushinMessageUnitConverter) {
		this.realSyushinMessageUnitConverter = realSyushinMessageUnitConverter;
	}

	public void setBatchSyushinMessageUnitConverter(
			UnitConverter batchSyushinMessageUnitConverter) {
		this.batchSyushinMessageUnitConverter = batchSyushinMessageUnitConverter;
	}

	public void setRealMessageFilenameLen(int realMessageFilenameLen) {
		this.realMessageFilenameLen = realMessageFilenameLen;
	}

	public void setBucketMessageFilenameLen(int bucketMessageFilenameLen) {
		this.bucketMessageFilenameLen = bucketMessageFilenameLen;
	}

}
